package com.example.administrator.yuejian_2.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.activity.CoDetailActivity;
import com.example.administrator.yuejian_2.bean.ContestListBean;
import com.example.administrator.yuejian_2.viewholder.CoListVH;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/11.
 */

public class CoListAdapter extends RecyclerView.Adapter {
    private List<ContestListBean> conList = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;

    public CoListAdapter(Context context,List<ContestListBean> conList){
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.conList.addAll(conList);

    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.item_co,parent,false);
        CoListVH holder = new CoListVH(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        CoListVH coholder = (CoListVH) holder;
        ContestListBean contestListBean = conList.get(position);

        final String name = contestListBean.getCon_title();
        coholder.coname.setText(name);

        final int i = contestListBean.getLiked();
        String like = String.valueOf(i);
        coholder.colikesum.setText(like);

        String pic = contestListBean.getCon_pic();
        if (!pic.equals(""))
            Picasso.with(context).
                    load("http://10.0.2.2/YueJian16/Public/contestphoto/"+pic)
                    .into(coholder.copic);

        final int cid = contestListBean.getCon_id();
        coholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CoDetailActivity.class);
                intent.putExtra("con_id",cid);
                context.startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount() {

        return conList.size();
    }
}
